<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>商品备案</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'/>
    <%@include file="/WEB-INF/pages/common/taglib.jsp" %>
    <%@include file="/WEB-INF/pages/common/common.jsp" %>
</head>
<body>
<div class="content content_frame">
    <h3 class="mainTitle"><img src="${basePath}/images/icon2.png"/>商品备案</h3>
    </br>
    <ul class="contentNav dib-wrap">
        <li class="dib" id="btnSearch" defaultkey='13'>
            <a><i class='fa fa-search'></i>&nbsp;&nbsp;查&nbsp;询 </a>
        </li>
        <li class="dib" id="btnAdd">
            <a><i class='fa fa-plus'></i>&nbsp;&nbsp;新&nbsp;增 </a>
        </li>
        <li class="dib" id="btnModify">
            <a><i class='fa fa-pencil'></i>&nbsp;&nbsp;修&nbsp;改 </a>
        </li>
        <li class="dib" id="btnDelete">
            <a><i class='fa fa-remove'></i>&nbsp;&nbsp;删&nbsp;除 </a>
        </li>
        <li class="dib" id="btnDeclare">
            <a><i class='fa fa-book'></i>&nbsp;&nbsp;备&nbsp;案 </a>
        </li>
        <li class="dib ">
            <a onclick="ClearAllInput('SearchTable')">
                <i class='fa fa-refresh'></i>&nbsp;&nbsp;重&nbsp;置 </a>
        </li>
    </ul>
    <br/>
    <table id="SearchTable" width="100%" class="table1">
        <tr>
            <th width="130" class="titleCss130">
                商品备案流水号
            </th>
            <td>
                <input type='text'
                       datafield="SearchTable.goodsserialno"/>
            </td>
            <th width="130" class="titleCss130">

                企业备案号
            </th>
            <td>
                <input type='text'
                       datafield="SearchTable.entcode"/>
            </td>
            <th width="130" class="titleCss130">

                商品货号
            </th>
            <td>
                <input type='text'
                       datafield="SearchTable.goodsskuno"/>

            </td>
            <th width="130" class="titleCss130">

                商品名称
            </th>
            <td>
                <input type='text' style="width: 100%;"
                       datafield="SearchTable.goodsname"/>

            </td>
        </tr>
        <tr>
            <th width="130" class="titleCss130">
                进出口标志
            </th>
            <td>
                <ui:tsingletableautocomplete
                        keyFieldName="ieflag"
                        valueFieldName="ieflagXXXX"
                        tableName="DICT_ITEM"
                        key="ITEM_KEY"
                        value="ITEM_VALUE"
                        groupId="ieType"
                        defaultKey=""
                        defaultValue=""
                        />

            </td>
            <th width="130" class="titleCss130">

                备案时间
            </th>
            <td>
                <input type='text' class="Wdate" id="applytimeStart"
                       onclick="WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'#F{$dp.$D(\'applytimeEnd\')}'})"
                       timeformat="YYYY-MM-DD"
                       datafield="SearchTable.applytimeStart"/>

            </td>
            <th width="130" class="titleCss130">

                至
            </th>
            <td>
                <input type='text' class="Wdate" id="applytimeEnd"
                       onclick="WdatePicker({dateFmt:'yyyy-MM-dd',minDate:'#F{$dp.$D(\'applytimeStart\')}'})"
                       timeformat="YYYY-MM-DD" datafield="SearchTable.applytimeEnd"/>

            </td>
            <th width="130" class="titleCss130">
                备案状态
            </th>
            <td>
                <ui:tsingletableautocomplete
                        keyFieldName="status"
                        valueFieldName="statusxxxx"
                        tableName="DICT_ITEM"
                        key="ITEM_KEY"
                        value="ITEM_VALUE"
                        groupId="goodsBackupStatus"
                        defaultKey=""
                        defaultValue=""/>

            </td>
        </tr>
    </table>
    <div class="cl"></div>
    <br/>
    <table id="corptable">
    </table>
    <div id="corptablePage">
    </div>
</div>


<script type="text/javascript">
    function showDetail(cellvalue, options, rowObject) {
        cellvalue = cellvalue || '';
        cellvalue = $.trim(cellvalue);
        var url = '${basePath}/goodsManage/goodsBackupInsp/initEdit?id=' + rowObject.id + "&r=" + Math.random();
        return "<a class='btn-link' data-value=\"" + cellvalue + "\" href='" + url + "'><span style='color:#00acec'>" + cellvalue + "</span></a>";
    }
    function getId(gridId) {
        var selectIDs = $("#" + gridId + "").getGridParam("selarrrow");
        var rows = [];
        for (var rowId in selectIDs) {
            var _row = $("#" + gridId + "").jqGrid("getRowData", selectIDs[rowId]);
            rows.push(_row.id);
        }

        return rows;
    }
    function getRows(gridId) {
        var selectIDs = $("#" + gridId + "").getGridParam("selarrrow");
        var rows = [];
        for (var rowId in selectIDs) {
            rows.push($("#" + gridId + "").jqGrid("getRowData", selectIDs[rowId]));
        }

        return rows;
    }

    $(function () {
        $("#corptable").jqGrid({
            datatype: "json",
            url: "page",
            colNames: [
                '',
                '商品备案流水号',
                '企业备案号',
                '商品货号',
                '商品名称',
                '进出口标志',
                '备案时间',
                '备案状态',
                ''
            ],
            colModel: [
                {name: 'id', index: 'id', hidden: true},
                {name: 'goodsserialno', index: 'goodsserialno', formatter: showDetail},
                {name: 'entcode', index: 'entcode'},
                {name: 'goodsskuno', index: 'goodsskuno'},
                {name: 'goodsname', index: 'goodsname'},
                {
                    name: 'ieflag', index: 'ieflag', formatter: function (cellvalue, options, rowObject) {
                    cellvalue = cellvalue || '';
                    cellvalue = $.trim(cellvalue);

                    switch (cellvalue) {
                        case "I":
                            return "进口";
                        case "E":
                            return "出口";
                        default:
                            return "";
                    }
                }
                },
                {name: 'applytime', index: 'applytime'},
                {name: 'statusDisplay', index: 'statusDisplay', width: '150px'},
                {name: 'status', index: 'status', hidden: true}
            ],
            height: 'auto',
            shrinkToFit: false,
            autoScroll: true,
            rowNum: 10, //每页显示记录数
            rowList: [10, 20, 100], //可调整每页显示的记录数
//            rownumbers: true,
            multiselect: true, //是否支持多选
            multiselectWidth: 35,
            viewrecords: true, //是否显示行数
            pager: "#corptablePage", //分页工具栏
            altRows: true,
            altclass: 'someClass',
            sortable: true,
            sortname: 'applytime',
            sortorder: 'asc',
            width: $('body').width(),
            loadComplete: function () {
                var rowNum = $(this).jqGrid('getGridParam', 'records');
                if (rowNum <= 0) {
                    if ($(this).parent().children('.norecords').length == 0)
                        $(this).parent().append("<div class=\"norecords\">暂无数据！</div>");
                    $(this).parent().children('.norecords').show();
                } else {//如果存在记录，则隐藏提示信息。
                    $(this).parent().children('.norecords').hide();
                }
                $("#" + this.id).setGridWidth($(window).width() * 0.98);
                $(".ui-jqgrid tr.ui-row-ltr td").css('text-align', 'center');
            }
        });

        //查询
        $("#btnSearch").click(function () {
            var jsonStr = customFormToData("SearchTable");
            $("#corptable").jqGrid('setGridParam', {
                url: "page", postData: JSON.parse(jsonStr), page: 1
            }).trigger("reloadGrid"); //重新载入
        });

        $("#btnAdd").click(function () {
            location.href = "${basePath}/goodsManage/goodsBackupInsp/initEdit?r=" + Math.random();
        });

        $("#btnModify").click(function () {
            var jsonArr = getId("corptable");
            if (0 == jsonArr.length) {
                layerAlert("对不起,您选择要删除的记录");
                return;
            }
            if (jsonArr.length > 1) {
                layerAlert("对不起,您只能选中一条记录");
                return;
            }

            var _rows = getRows("corptable");
            for (var i in _rows) {
                var _status = _rows[i]['status'];
                if (_status == 'E0' || _status == '4' || _status == '7' || _status == '9') {
                    continue;
                }

                layerAlert("对不起只有暂存成功、一级审核否决、二级审核否决、电子审单未通过状态的记录才能修改");
                return;
            }

            location.href = "${basePath}/goodsManage/goodsBackupInsp/initEdit?id=" + jsonArr[0] + "&r=" + Math.random();
        });

        $("#btnDelete").click(function () {
            var selectedId = $("#corptable").jqGrid("getGridParam", "selrow");
            if (!selectedId) {
                layerAlert("对不起,您没有选中任何记录");
                return;
            }

            var rowIds = $("#corptable").getGridParam("selarrrow");
            var len = rowIds.length;
            for (var i = 0; i < len; i++) {
                var row = $("#corptable").jqGrid("getRowData", rowIds[i]);
                if (row.status != '' && row.status != 'E0') {
                    layerAlert("只允许删除暂存成功的数据");
                    return false;
                }
            }

            showConfirm("确定要删除选中的记录吗?", "系统提示", function () {
                var selectedRowIds = $("#corptable").getGridParam("selarrrow");
                var len = selectedRowIds.length;
                var jsonSeqId = "";
                for (var i = 0; i < len; i++) {
                    jsonSeqId += $("#corptable").jqGrid("getRowData", selectedRowIds[i]).id;
                    jsonSeqId += ",";
                }
                jsonSeqId = jsonSeqId.substr(0, jsonSeqId.length - 1);
                $.ajax({
                    type: "post",
                    async: false,
                    dataType: "json",
                    url: '${basePath}/goodsManage/goodsBackupInsp/deleteHeadInsp',
                    data: {"id": jsonSeqId + ""},
                    success: function (result) {
                        if (result) {
                            layerAlertOk('删除成功!', function () {
                                var jsonStr = customFormToData("SearchTable");
                                $("#corptable").jqGrid('setGridParam', {
                                    url: "${basePath}/goodsManage/goodsBackupInsp/page",
                                    postData: JSON.parse(jsonStr),
                                    page: 1
                                }).trigger("reloadGrid"); //重新载入
                            });
                        } else {
                            layerAlert("删除失败");
                        }
                    }
                });
            })
        });

        $("#btnDeclare").click(function declareEnt() {
            var _rows = getRows("corptable");
            if (_rows.length < 1) {
                layerAlert("对不起,请选择要备案的记录");
                return;
            }

            for (var i in _rows) {
                var _status = _rows[i]['status'];
                if (_status == 'E0' || _status == '4' || _status == '7' || _status == '9') {
                    continue;
                }

                layerAlert("对不起只有暂存成功、一级审核否决、二级审核否决、电子审单未通过状态的记录才能备案");
                return;
            }

            var _data = getId("corptable");
            var _dataStr = "id=" + _data.join("&id=").toString();
            $.ajax({
                type: "post",
                url: '${basePath}/goodsManage/goodsBackupInsp/declare',
                datatype: "json",
                data: _dataStr,
                success: function (result) {
                    if (result['code'] + '' == '1') {
                        layerAlertOk('备案数据已提交申请！', function () {
                            var jsonStr = customFormToData("SearchTable");
                            $("#corptable").jqGrid('setGridParam', {
                                url: $basePath + "/goodsManage/goodsBackupInsp/page",
                                postData: JSON.parse(jsonStr),
                                page: 1
                            }).trigger("reloadGrid"); //重新载入
                        });
                        return;
                    }

                    layerAlert("备案失败" + result['msg']);
                },
                error: function () {
                    layerAlert("备案失败");
                }
            });
        });
    });
</script>
</body>
</html>
